package com.jdk.learn.juc2.thread.excrise;

import java.util.concurrent.CountDownLatch;

/**
 * 类功能描述:
 * 等待线程A和B处理完业务
 * 有问题 会死锁
 *
 * @author bgt
 * @version V1.0
 * @since 2019-02-21 13:39
 */
public class PrintABByCountDownLatch {
//    private static volatile CountDownLatch countDownLatch = new CountDownLatch(20);
    private static volatile CountDownLatch countDownLatch = new CountDownLatch(30);

    public static void main(String[] args) throws InterruptedException {
//        new Thread(new MyRunnable("A", 0)).start();
//        new Thread(new MyRunnable("B", 1)).start();
        new Thread(new MyRunnable("A", 0)).start();
        new Thread(new MyRunnable("B", 2)).start();
        new Thread(new MyRunnable("C", 1)).start();
        countDownLatch.await();
    }


    static class MyRunnable implements Runnable {
        private String printStr;
        private Integer printNum;

        public MyRunnable(String printStr, Integer printNum) {
            this.printStr = printStr;
            this.printNum = printNum;
        }

        @Override
        public void run() {
            while (countDownLatch.getCount()!=0) {
                if (countDownLatch.getCount() % 3 == printNum) {
                    System.out.print(printStr);
                    countDownLatch.countDown();
                }
            }
        }
    }
}
